﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TimeTracker_Entities;
using System.Configuration;

namespace TimeTracker_BL.Repositories
{
    public class LookupsRepository : ILookupsRepository
    {
        /// <summary>
        /// Gets the billing terms.
        /// </summary>
        /// <returns>A list of <see cref="BillingTerm"/> objects</returns>
        public IList<BillingTerm> GetBillingTerms()
        {
            List<BillingTerm> billingTerms = new List<BillingTerm>();

            using (var ctx = new TimeTrackerEntitiesContainer())
            {
                // Get an ordered list of Billing Terms
                var terms = from t in ctx.BillingTerms
                            orderby t.BillingTermsDescription
                            select t;

                billingTerms = terms.ToList<BillingTerm>();
            }

            return billingTerms;
        }
        
        /// <summary>
        /// Adds the billing term to the database.
        /// </summary>
        /// <param name="termToAdd">The term to add.</param>
        /// <returns>
        /// An <see cref="IOpResult"/> object containing data including whether or 
        /// not the operation was successful and any error messages.
        /// </returns>
        public IOpResult AddBillingTerm(BillingTerm termToAdd)
        {
            var result = new OpResult();

            try
            {
                using (var ctx = new TimeTrackerEntitiesContainer())
                {
                    // Add the object
                    ctx.BillingTerms.AddObject(termToAdd);
                    ctx.SaveChanges();

                    // Return success
                    result.IsSuccessful = true;
                    return result;
                }
            }
            catch (Exception ex)
            {
                // Return failure
                result.IsSuccessful = false;
                result.ErrorMessage = ex.Message;
                return result;
            }
        }

        /// <summary>
        /// Updates the billing term.
        /// </summary>
        /// <param name="updatedTerm">The updated term.</param>
        /// <returns>
        /// An <see cref="IOpResult"/> object containing data including whether or
        /// not the operation was successful and any error messages.
        /// </returns>
        public IOpResult UpdateBillingTerm(BillingTerm updatedTerm)
        {
            var result = new OpResult();

            try
            {
                using (var ctx = new TimeTrackerEntitiesContainer())
                {
                    // Update the object
                    ctx.AttachTo("BillingTerms", updatedTerm);
                    ctx.ApplyCurrentValues<BillingTerm>("BillingTerms", updatedTerm);
                    ctx.SaveChanges();

                    // Return success
                    result.IsSuccessful = true;
                    return result;
                }
            }
            catch (Exception ex)
            {
                // Return failure
                result.IsSuccessful = false;
                result.ErrorMessage = ex.Message;
                return result;
            }
        }

        /// <summary>
        /// Deletes the billing term.
        /// </summary>
        /// <param name="billingTermId">The billing term id.</param>
        /// <returns>
        /// An <see cref="IOpResult"/> object containing data including whether or 
        /// not the operation was successful and any error messages.
        /// </returns>
        public IOpResult DeleteBillingTerm(int deletedTermId)
        {
            var result = new OpResult();

            try
            {
                using (var ctx = new TimeTrackerEntitiesContainer())
                {
                    // Delete the object
                    var term = ctx.BillingTerms.First(t => t.BillingTermsId == deletedTermId);
                    ctx.BillingTerms.DeleteObject(term);
                    ctx.SaveChanges();

                    // Return success
                    result.IsSuccessful = true;
                    return result;
                }
            }
            catch (Exception ex)
            {
                // Return failure
                result.IsSuccessful = false;
                result.ErrorMessage = ex.Message;
                return result;
            }
        }
    }
}
